package com.xinsoft.controller.sale;

import com.alibaba.fastjson.JSON;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.ScmDistributionDTO;
import com.xinsoft.entity.dto.ScmDistributionParam;
import com.xinsoft.entity.vo.ScmDistributionVo;
import com.xinsoft.service.ScmDistributionService;
import com.xinsoft.validator.ValidatorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 云供应链/销售管理/车辆配送
 */
@Slf4j
@RestController
@RequestMapping("/scm/distribution")
public class ScmDistributionController {
    @Autowired
    private ScmDistributionService distributionService;

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody ScmDistributionParam param) {
        return Response.succeed(distributionService.findPageListByParam(param));
    }

    /**
     * 暂存
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody ScmDistributionDTO distributionDTO) {
        log.info("车辆配送暂存参数：{}", JSON.toJSONString(distributionDTO));
        // 参数校验
        ValidatorUtils.validateEntity(distributionDTO);
        distributionService.tempSave(distributionDTO);
        return Response.succeed();
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<ScmDistributionVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        return Response.succeed(distributionService.detail(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        List<Integer> newIds = ids.stream().distinct().collect(Collectors.toList());
        distributionService.del(newIds);
        return Response.succeed();
    }

    /**
     * 批量确认收货
     */
    @PostMapping("/batchConfirmReceipt")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> batchConfirmReceipt(@RequestBody List<Integer> ids){
        distributionService.batchConfirmReceipt(ids.stream().distinct().collect(Collectors.toList()));
        return Response.succeed();
    }

}
